﻿namespace IntellectShop.Common
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Transactions;

	public partial class Feedback
	{
		public static Feedback RetrieveByFeedbackID(int feedbackID)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var query = (
							from feedback in dataContext.Feedbacks
							where feedback.FeedbackID == feedbackID
							select feedback).SingleOrDefault();

				Feedback item = new Feedback
				{
					FeedbackID = query.FeedbackID,
					FeedbackOwnerID = query.FeedbackOwnerID,
					Author = query.Author,
					Email = query.Email,
					Body = query.Body,
					CreatedBy = query.CreatedBy,
					Created = query.Created,
					Updated = query.Updated,
					UpdatedBy = query.UpdatedBy
				};

				return item;
			}
		}

		public static List<Common.Feedback> RetrieveAll()
		{
			using (DB.IntellectShopDataContext context = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var query =
							from feedback in context.Feedbacks
							orderby feedback.Created descending, feedback.FeedbackID descending
							select feedback;

				List<Common.Feedback> result = new List<Feedback>();
				foreach (var row in query)
				{
					result.Add(new Common.Feedback
					{
						FeedbackID = row.FeedbackID,
						FeedbackOwnerID = row.FeedbackOwnerID,
						Author = row.Author,
						Email = row.Email,
						Body = row.Body,
						CreatedBy = row.CreatedBy,
						Created = row.Created,
						Updated = row.Updated,
						UpdatedBy = row.UpdatedBy
					});
				}

				return result;
			}
		}

		public void Create(string createdBy)
		{
			this.CreatedBy = createdBy;
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var feedbackDB = new DB.Feedback
				{
					FeedbackID = this.FeedbackID,
					FeedbackOwnerID = this.FeedbackOwnerID,
					Author = this.Author,
					Email = this.Email,
					Body = this.Body,
					CreatedBy = this.CreatedBy,
					Created = DateTime.Now
				};
				dataContext.Feedbacks.InsertOnSubmit(feedbackDB);
				dataContext.SubmitChanges();
				this.FeedbackID = feedbackDB.FeedbackID;
			}
		}

		public void Update(string updatedBy)
		{
			this.UpdatedBy = updatedBy;
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var feedbackDB = (from feedback in dataContext.Feedbacks where feedback.FeedbackID == this.FeedbackID select feedback).SingleOrDefault();
				feedbackDB.FeedbackOwnerID = this.FeedbackOwnerID;
				feedbackDB.Author = this.Author;
				feedbackDB.Email = this.Email;
				feedbackDB.Body = this.Body;
				feedbackDB.UpdatedBy = this.UpdatedBy;
				feedbackDB.Updated = DateTime.Now;

				dataContext.SubmitChanges();
			}
		}

		public static void Delete(int feedbackID)
		{
			using (TransactionScope trans = new TransactionScope())
			{
				using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
				{
					var feedbackDB = (from feedback in dataContext.Feedbacks where feedback.FeedbackID == feedbackID select feedback).SingleOrDefault();
					dataContext.Feedbacks.DeleteOnSubmit(feedbackDB);
					dataContext.SubmitChanges();

					trans.Complete();
				}
			}
		}
	}
}
